package com.bajiao.admin.controller.giveRule;

import java.util.Arrays;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.StrUtil;
import com.bajiao.common.api.vo.Result;
import com.bajiao.entity.coupon.Coupon;
import com.bajiao.entity.giveRule.GiveRule;
import com.bajiao.service.giveRule.GiveRuleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import com.bajiao.admin.base.AdminBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 赠送规则
 * @Author: FanJq
 * @Date:   2024-12-12
 * @Version: V1.0
 */
@Slf4j
@Api(tags="赠送规则")
@RestController
@RequestMapping("/give/rule")
public class GiveRuleController extends AdminBaseController<GiveRule, GiveRuleService> {

	@Resource
	private GiveRuleService giveRuleService;

	/**
	 * @description: 分页列表查询
	 * @author: FanJq
	 * @date:   2024-12-12
	 * @return
	 */
	@ApiOperation(value="赠送规则-分页列表查询", notes="赠送规则-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(String title, String code) {
		LambdaQueryWrapper<GiveRule> sqlQuery = new LambdaQueryWrapper<>();
        startPage();
		sqlQuery.like(StrUtil.isNotBlank(title), GiveRule::getTitle, title);
		sqlQuery.like(StrUtil.isNotBlank(code), GiveRule::getCode, code);
		sqlQuery.orderByDesc(GiveRule::getCreateTime);
        return Result.ok(getDataTable(giveRuleService.list(sqlQuery)));
	}

	/**
	 * @description: 添加
	 * @author: FanJq
	 * @date:   2024-12-12
	 * @param giveRule
	 * @return
	 */
	@ApiOperation(value="赠送规则-添加", notes="赠送规则-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody GiveRule giveRule) {
		Integer count = giveRuleService.lambdaQuery().eq(GiveRule::getCode, giveRule.getCode()).count();
		if (count > 0) return Result.error("编码已存在");
		giveRuleService.save(giveRule);
		return Result.ok("添加成功！");
	}

	/**
	 * @description: 编辑
	 * @author: FanJq
	 * @date:   2024-12-12
	 * @param giveRule
	 * @return
	 */
	@ApiOperation(value="赠送规则-编辑", notes="赠送规则-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody GiveRule giveRule) {
		giveRuleService.updateById(giveRule);
		return Result.ok("编辑成功!");
	}

	 @ApiOperation(value="赠送规则-开关", notes="赠送规则-开关")
	 @PutMapping(value = "/handle/switch")
	 public Result<?> handleSwitch(@RequestBody GiveRule request) {
		 GiveRule rule = giveRuleService.getById(request.getId());
		 Integer isSwitch = rule.getIsSwitch() == 0 ? 1 : 0;
		 giveRuleService.lambdaUpdate().set(GiveRule::getIsSwitch, isSwitch).eq(GiveRule::getId, rule.getId()).update();
		 return Result.ok(isSwitch == 0 ? "已关闭" : "已开启");
	 }

	/**
	 * @description: 通过id删除
	 * @author: FanJq
	 * @date:   2024-12-12
	 * @param id
	 * @return
	 */
	@ApiOperation(value="赠送规则-通过id删除", notes="赠送规则-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		giveRuleService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * @description: 批量删除
	 * @author: FanJq
	 * @date:   2024-12-12
	 * @param ids
	 * @return
	 */
	@ApiOperation(value="赠送规则-批量删除", notes="赠送规则-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.giveRuleService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}

	/**
	 * @description: 通过id查询
	 * @author: FanJq
	 * @date:   2024-12-12
	 * @param id
	 * @return
	 */
	@ApiOperation(value="赠送规则-通过id查询", notes="赠送规则-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		GiveRule giveRule = giveRuleService.getById(id);
		return Result.ok(giveRule);
	}

  /**
   * @description: 导出excel
   * @author: FanJq
   * @date:   2024-12-12
   * @param request
   * @param giveRule
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, GiveRule giveRule) {
      return super.exportXls(request, giveRule, GiveRule.class, "赠送规则");
  }

  /**
   * @description: 通过excel导入数据
   * @author: FanJq
   * @date:   2024-12-12
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, GiveRule.class);
  }

}
